#include "xAnimationSet.h"
#include "xLogManager.h"

namespace XE
{


	xAnimationSet::xAnimationSet(const xString& name)
		:m_Name(name.c_str()),m_leng(0.0f),m_lasttime(0.0f),m_Factor(1.0f)
	{

	}
	xAnimationSet::~xAnimationSet()
	{

		DestoryAllAnimation();
	}

	xNodeAnimation* xAnimationSet::CreateNodeAnimation(const xString& name)
	{
		if(GetNodeAnimationByName(name)!=NULL)
		{
			xLogMessager::getSingleton().logMessage(xString("has same name animation in animationset:")+name);
			return GetNodeAnimationByName(name);
		}
		xNodeAnimation*pAni =  new xNodeAnimation(name,this);
		if(pAni !=NULL)
		{
			m_AnimationVector.push_back(pAni);
		}
		return pAni;
	}

	xNodeAnimation* xAnimationSet::GetNodeAnimationByName(const xString& name)
	{

		size_t size = m_AnimationVector.size();
		for(size_t i = 0;i<size;++i)
		{
			if(m_AnimationVector.at(i)->GetNodeName()==name)
			{
				return m_AnimationVector.at(i);
			}
		}
		return NULL;

	}

	xNodeAnimation* xAnimationSet::GetNodeAniamtionByIndex(UINT index)
	{
		if(index>m_AnimationVector.size()-1)
			return NULL;
		return m_AnimationVector.at(index);
	}


	void xAnimationSet::Addtime(float time)
	{
		m_lasttime+=time;
		if(m_lasttime>m_leng)
			m_lasttime  =0 ;


		size_t size = m_AnimationVector.size();
		for(size_t i = 0;i<size;++i)
		{
			xNodeAnimation* pAni = m_AnimationVector[i];
			if(pAni!=NULL)
			{
				pAni->SetTime(m_lasttime,m_Factor);
			}
		}

		return;


	}

	void xAnimationSet::_notifyLengthChange(float time)
	{
		m_leng = max(time,m_leng);
	}



	void xAnimationSet::DestoryAllAnimation()
	{

		xNodeAnimationVector::iterator it = m_AnimationVector.begin();
		xNodeAnimationVector::iterator itend = m_AnimationVector.end();
		for(;it!=itend;++it)
		{
			SafeDelete(*it)
		}
		m_AnimationVector.clear();
	}

}